﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XNA_TD.Shells;

namespace XNA_TD
{
    public class MGTower : Tower
    {
        static ModelBone towerBone = null;
        static LvlInfo startInfo;
        static ModelBone turretBone;
        static ModelBone[] gunBones;

        static Matrix towerTransform;
        static Matrix turretTransform;
        static Matrix[] boneTransforms;
        static Matrix[] gunTransforms;

        double[] gunRecoil;

        int gunIndex = 0;

        public MGTower(Vector3 pos)
            : base(pos, "Tower/MGTower")
        {
            if (towerBone == null)
            {
                towerBone = model.Bones["MGTower"];
                turretBone = model.Bones["MGTowerTurret"];

                gunBones = new ModelBone[2];
                gunBones[0] = model.Bones["MGTowerGunLeft"];
                gunBones[1] = model.Bones["MGTowerGunRight"];

                towerTransform = towerBone.Transform;
                turretTransform = turretBone.Transform;

                gunTransforms = new Matrix[2];
                gunTransforms[0] = gunBones[0].Transform;
                gunTransforms[1] = gunBones[1].Transform;

                boneTransforms = new Matrix[model.Bones.Count];

                startInfo = new LvlInfo("", 1, 4, 100, 100, 200, GegnerTyp.Boden | GegnerTyp.Wasser);
                var last = startInfo
                    .Add("Speed Upgrade", 1, 4, 140, 100, 100, GegnerTyp.Boden | GegnerTyp.Wasser);
                last.Add("Speed Upgrade", 1, 4, 200, 150, 100, GegnerTyp.Boden | GegnerTyp.Wasser);
                last.Add("Damage Upgrade", 2, 4, 140, 150, 200, GegnerTyp.Boden | GegnerTyp.Wasser);

                var antiAir = startInfo
                    .Add("Anti Air Upgrade", 1, 10, 100, 100, 200, GegnerTyp.Luft);
                antiAir.Add("Speed Upgrade", 1, 10, 200, 100, 200, GegnerTyp.Luft);
                antiAir.Add("Damage Upgrade", 2, 10, 100, 100, 200, GegnerTyp.Luft);
            }
            gunRecoil = new double[2];
            gunRecoil[0] = 0;
            gunRecoil[1] = 0;

            turretYawSpeed = 0.015f;
            turretPitchSpeed = 0.015f;

            lvlInfo = startInfo;

            ResetHealth();
        }

        public override void FixedGameUpdate()
        {
            base.FixedGameUpdate();
            gunRecoil[0] = Math.Max(gunRecoil[0] - 0.002f, 0);
            gunRecoil[1] = Math.Max(gunRecoil[1] - 0.002f, 0);
        }

        protected override Matrix[] CalculateAbsoluteBoneTransforms()
        {
            turretBone.Transform = Matrix.CreateRotationX(turretPitch) * turretTransform;
            towerBone.Transform = Matrix.CreateRotationZ(turretYaw) * towerTransform;

            gunBones[0].Transform = Matrix.CreateTranslation(0, (float)gunRecoil[0], 0) * gunTransforms[0];
            gunBones[1].Transform = Matrix.CreateTranslation(0, (float)gunRecoil[1], 0) * gunTransforms[1];

            model.Root.Transform = root * Matrix.CreateTranslation(position);
            model.CopyAbsoluteBoneTransformsTo(boneTransforms);

            return boneTransforms;
        }

        protected override void Shot()
        {
            gunIndex = (gunIndex + 1) % gunBones.Length;
            gunRecoil[gunIndex] = 0.1;
            CalculateAbsoluteBoneTransforms();

            Vector3 translation = boneTransforms[gunBones[gunIndex].Index].Translation;
            Register(new MGTowerShell(translation, target, lvlInfo.attackDmg));
        }
    }
}
